<?php


namespace App\Http\Controllers\Mini\Shop;

use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Services\CartService;
use App\Services\UserService;
use Illuminate\Http\Request;

/**
 * 购物车
 * Class CartController
 * @package App\Http\Controllers\Mini
 */
class CartController extends Controller
{
    public $cartModel;

    /**
     * 构造方法
     * CartController constructor.
     */
    public function __construct()
    {
        $this->cartModel = new CartService();
    }


    /**
     * 获取购物车列表
     * @return array
     */
    public function getCartList()
    {
        try {
            return $this->cartModel->getCarts();
        } catch (\Exception $e) {
            return $this->format_error('获取失败:' . $e->getMessage(), '');
        }
    }


    /**
     * @return array
     */
    public function cartChecked()
    {
        try {
            return $this->cartModel->cartAction();
        } catch (\Exception $e) {
            return $this->format_error('选中失败:' . $e->getMessage(), '');
        }
    }

    /**
     * 添加购物车
     * @return array
     */
    public function addCart()
    {
        try {
            return $this->cartModel->addCart();
        } catch (\Exception $e) {
            return $this->format_error('添加失败:' . $e->getMessage(), '');
        }
    }

    /**
     * @param Request $request
     * @return array
     * @cart_id
     */
    public function editCart(Request $request)
    {
        try {
            return $this->cartModel->editCart($request->cart_id);
        } catch (\Exception $e) {
            return $this->format_error('修改失败:' . $e->getMessage(), '');
        }
    }

    /**
     * 删除购物车接口
     */
    public function delCart(Cart $cart_model)
    {
        try {
            $idArray = array_filter(explode(',', request()->ids), function ($item) {
                return is_numeric($item);
            });
            // 获取当前用户user_id
            $user_service = new UserService;
            if (!$user_info = $user_service->getUserInfo()) {
                return $this->format_error(__('carts.add_error') . '4'); // 获取用户失败
            }
            $cart_model->whereIn('id', $idArray)->where('user_id', $user_info['id'])->delete();
            return $this->format([], __('base.success'));
        } catch (\Exception $e) {
            return $this->format_error($e->getMessage());
        }
    }


    /**
     * 清空购物车
     * @param Cart $cart_model
     * @return array
     */
    public function clearCart(Cart $cart_model)
    {
        $user_service = new UserService;
        if (!$user_info = $user_service->getUserInfo()) {
            return $this->format_error(__('carts.add_error') . '4'); // 获取用户失败
        }
        $cart_model->where('user_id', $user_info['id'])->delete();
        return $this->format([], __('base.success'));
    }


    /**
     * 2021/3/18--9:46
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:cart_count
     * @effect:获取购物车数量
     */
    public function cart_count()
    {
        $cart_service = new CartService;
        $rs = $cart_service->getCount();
        return $rs['status'] ? $this->success($rs['data'], $rs['msg']) : $this->error($rs['msg']);
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
        $this->cartModel;
    }
}
